{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 注意！请根据提示操作！\n",
    "\n",
    "如果当页面加载完成，你还是看到了这条提示\n",
    "\n",
    "那么你的notebook并未被信任，导致代码无法正常运行\n",
    "\n",
    "点击右上角的`不可信`，将本notebook列入可信列表，即可正常运行。\n",
    "\n",
    "![可信按钮](./mgr/trust.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 时间日期处理\n",
    "\n",
    "## 时间戳\n",
    "\n",
    "要处理时间，首先我们得理解时间是如何保存在计算机中的。\n",
    "\n",
    "计算机中，是以时间戳的形式保存着时间。\n",
    "\n",
    "时间戳是一个浮点数（也就是小数，有的方法会获取到整数），time模块就可以获取时间戳，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "print(time.time())  # 获取当前时间戳"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个浮点数，保存的是自1970年1月1日凌晨0点0分0秒，到现在为止经过的秒数。计算机就是用这样的方法保存着时间。\n",
    "\n",
    "## datetime\n",
    "\n",
    "但是时间戳这种方式保存的时间对于人类而言太不友好，所以有的时候我们需要对这些进行转换。datetime模块中就包含了很多方便处理的功能："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "\n",
    "n = datetime.datetime.now()  # datetime模块中还有一个同名的子模块，所以需要datetime2次。n获取到的不是字符串，而是保存当前时间的datetime的对象。\n",
    "print(n)\n",
    "\n",
    "print(f'今年是{n.year}年')\n",
    "print(f'这个月是{n.month}月')\n",
    "print(f'今天是{n.day}号，一周的第{n.weekday() + 1}天')\n",
    "print(f'现在是{n.hour}点{n.minute}分{n.second}秒')\n",
    "\n",
    "# 当然还能将他们经过格式化后以不同格式输出，其中%Y代表年，%m代表月，%d代表日，%H代表时，%M代表分，%S代表秒。都大小写敏感。\n",
    "print(n.strftime('日期：%Y年%m月%d日 时间：%H点%M分%S秒'))\n",
    "\n",
    "# 当然也是能输出时间戳的\n",
    "print(n.timestamp())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有的时候我们只有时间戳，也可以将其转化成datetime对象，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "t = 569392728.98\n",
    "d = datetime.datetime.fromtimestamp(t)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "或者只知道具体时间，也可以转化成datetime对象，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "t = '2012-3-18 12:23:45'\n",
    "d = datetime.datetime.strptime(t, '%Y-%m-%d %H:%M:%S')  # 注意第二个参数的格式要和第一个参数的格式完全匹配。\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还可以对datetime对象进行计算，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "t1 = datetime.datetime.strptime('2012-3-12 12:23:45', '%Y-%m-%d %H:%M:%S')\n",
    "t2 = datetime.datetime.strptime('2019-9-25 14:32:31', '%Y-%m-%d %H:%M:%S')\n",
    "delta = t2 - t1\n",
    "print(f'相隔：{delta.days}天，{delta.total_seconds()}秒')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "t1 = datetime.datetime.strptime('2012-3-12 12:23:45', '%Y-%m-%d %H:%M:%S')\n",
    "delta = datetime.timedelta(days=500, seconds=25)\n",
    "result = t1 + delta\n",
    "print(f'{t1} 的500天零25秒后为： {result}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<hr>\n",
       "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
       "<script src=\"./mgr/submit.js\"></script>\n",
       "<script src=\"./mgr/hide.js\"></script>\n",
       "<div class=\"mdui-container-fluid\">\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
       "    </div>\n",
       "    <div class=\"mdui-col-xs-3\">\n",
       "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
       "    </div>\n",
       "</div>\n",
       "<div id=\"html_output\">\n",
       "</div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<hr>\n",
    "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/mdui/0.4.3/css/mdui.min.css\">\n",
    "<script src=\"./mgr/submit.js\"></script>\n",
    "<script src=\"./mgr/hide.js\"></script>\n",
    "<div class=\"mdui-container-fluid\">\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-500\" onclick=\"next_page(-1)\">上一章</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-700\" onclick=\"window.open('/tree')\">目录</button>\n",
    "    </div>\n",
    "    <div class=\"mdui-col-xs-3\">\n",
    "        <button class=\"mdui-btn mdui-btn-raised mdui-ripple mdui-color-blue-grey-900\" onclick=\"next_page(1)\">下一章</button>\n",
    "    </div>\n",
    "</div>\n",
    "<div id=\"html_output\">\n",
    "</div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
